# Template file for 'basilisk'
pkgname=basilisk
version=2025.02.22
revision=1
_UXP_version=RB_20250219
create_wrksrc=yes
build_wrksrc="basilisk"
hostmakedepends="autoconf213 perl pkgconf python2 unzip yasm zip tar
 which"
makedepends="libXt-devel $(vopt_if alsa alsa-lib-devel)
 $(vopt_if dbus dbus-glib-devel) $(vopt_if pulseaudio pulseaudio-devel)
 $(vopt_if xscreensaver libXScrnSaver-devel) $(vopt_if sndio sndio-devel)
 $(vopt_if jack jack-devel) $(vopt_if gtk3 gtk+3-devel gtk+-devel)"
short_desc="Basilisk is a browser application built on the Unified XUL Platform"
maintainer="MarcoAPC <marcoaureliopc@gmail.com>"
license="MPL-2.0, GPL-2.0-or-later, LGPL-2.1-or-later"
homepage="https://www.basilisk-browser.org/"
distfiles="https://repo.palemoon.org/Basilisk-Dev/Basilisk/archive/v${version}.tar.gz
 https://repo.palemoon.org/MoonchildProductions/UXP/archive/${_UXP_version}.tar.gz"
checksum="69f58e7fa0764eb8446797bef0bec6be79129e2b3abdd7d5587ce8b506c60153
 faabd4c409b62f1a022b77f90b5e2d90e654db64fd435d1e8357a1ec6cb5b4d4"
patch_args="-Np1 --directory=${build_wrksrc}"

CFLAGS="-D_LARGEFILE64_SOURCE=1 -D_FILE_OFFSET_BITS=64"
CXXFLAGS="-D_LARGEFILE64_SOURCE=1 -D_FILE_OFFSET_BITS=64"

build_options="alsa jack dbus pulseaudio xscreensaver sndio gtk3"
build_options_default="alsa"

if [ "$XBPS_TARGET_NO_ATOMIC8" ]; then
	makedepends+=" libatomic-devel"
fi

post_extract() {
	rmdir basilisk/platform
	mv uxp basilisk/platform
}

post_patch() {
	if [ "$build_option_gtk3" ]; then
		patch -Np1 --directory=${build_wrksrc} -i "${FILESDIR}/no-gtk2.patch"
	fi

}

do_build() {
	cp "${FILESDIR}/mozconfig" ".mozconfig"

	cat <<! >>.mozconfig
ac_add_options --enable-default-toolkit=cairo-gtk$(vopt_if gtk3 '3' '2')
ac_add_options $(vopt_enable alsa)
ac_add_options $(vopt_enable jack)
ac_add_options $(vopt_enable sndio)
ac_add_options $(vopt_enable dbus)
ac_add_options $(vopt_enable pulseaudio)
export MOZ_PKG_SPECIAL=gtk$(vopt_if gtk3 '3' '2')
!

	case "$XBPS_TARGET_MACHINE" in
	*-musl)
		echo "ac_add_options --disable-jemalloc" >>.mozconfig
		echo "ac_add_options --disable-gold" >>.mozconfig
		;;
	esac

	case "$XBPS_TARGET_MACHINE" in
	x86_64*|i686*|arm*|aarch64*)
		echo "ac_add_options --disable-elf-hack" >>.mozconfig
		;;
	esac

	if [ "$XBPS_TARGET_NO_ATOMIC8" ]; then
		export LDFLAGS+=" -latomic"
	fi

	if [ "$CROSS_BUILD" ]; then
		export HOST_CFLAGS="${XBPS_CFLAGS}"
		export HOST_CXXFLAGS="${XBPS_CXXFLAGS}"
		export CXXFLAGS+=" ${CFLAGS}"
		export LDFLAGS+=" -L${XBPS_CROSS_BASE}/usr/lib"

		export ac_cv_sqlite_secure_delete=yes \
			ac_cv_sqlite_threadsafe=yes \
			ac_cv_sqlite_enable_fts3=yes \
			ac_cv_sqlite_dbstat_vtab=yes \
			ac_cv_sqlite_enable_unlock_notify=yes \
			ac_cv_prog_hostcxx_works=1

		echo "ac_add_options --target=${XBPS_CROSS_TRIPLET:-${XBPS_TRIPLET}}" >>.mozconfig
		echo "ac_add_options --host=${XBPS_TRIPLET}" >>.mozconfig
	else
		echo "ac_add_options --target=${XBPS_TRIPLET}" >>.mozconfig
		echo "ac_add_options --host=${XBPS_TRIPLET}" >>.mozconfig
	fi

	# work around large debug symbols on 32-bit hosts
	if [ "$XBPS_WORDSIZE" = "32" ]; then
		echo "ac_add_options --disable-debug-symbols" >>.mozconfig
		export LDFLAGS+=" -Wl,--no-keep-memory"
	fi

	if [ "$SOURCE_DATE_EPOCH" ]; then
		export MOZ_BUILD_DATE=$(date --date "@$SOURCE_DATE_EPOCH" "+%Y%m%d%H%M%S")
	fi

	export LDFLAGS+=" -Wl,-rpath=/usr/lib/basilisk"

	# increase default stack size as basilisk uses a recursive js parser
	export LDFLAGS+=" -Wl,-z,stack-size=524288"

	export MOZ_MAKE_FLAGS="${makejobs}"
	export MOZ_NOSPAM=1
	export MOZBUILD_STATE_PATH="${wrksrc}/mozbuild"

	rm -f old-configure
	./mach build
}

do_install() {
	export MOZBUILD_STATE_PATH="${wrksrc}/mozbuild"
	DESTDIR="$DESTDIR" ./mach install

	vinstall ${FILESDIR}/basilisk.desktop 644 usr/share/applications

	for i in 16 32 48; do
		vinstall basilisk/branding/unofficial/default$i.png 644 usr/share/icons/hicolor/${i}x${i}/apps basilisk.png
	done

	# We don't want the development stuff
	rm -rf ${DESTDIR}/usr/{include,lib/basilisk-devel}

	# https://bugzilla.mozilla.org/show_bug.cgi?id=658850
	ln -sf basilisk ${DESTDIR}/usr/lib/basilisk/basilisk-bin
}
